package b2.widget.friend
{
	import b2.net.*;
	import b2.player.*;
	import b2.player.dataModel.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.sendMail.*;
	import b2.widget.tank.*;
	import b2.widget.warehouse.*;
	
	import com.bit101.components.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;
	
	public class FriendItemMessage extends Sprite
	{
		private var friendItem:FriendItem;
		
		public function FriendItemMessage(friendItem:FriendItem)
		{
			super();
			this.friendItem = friendItem;
			var roundRec:RoundRec = new RoundRec(90, 65, 0.8, BFont.roundRecColor);
			addChild(roundRec);
			//显示信息
			var showText:TextField = new TextField();
			showText.selectable = false;
        	showText.autoSize = TextFieldAutoSize.LEFT;
			showText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x0000FF, true, null, true);
			showText.text = "攻击要塞";
			showText.x = 15;
			showText.y = 10;
			showText.addEventListener(MouseEvent.MOUSE_OVER, onTextMouseOver);
			showText.addEventListener(MouseEvent.MOUSE_OUT, onTextMouseOut);
			showText.addEventListener(MouseEvent.CLICK, showTankInfo);
			addChild(showText);
			//显示信息
			var sendText:TextField = new TextField();
			sendText.selectable = false;
        	sendText.autoSize = TextFieldAutoSize.LEFT;
			sendText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x0000FF, true, null, true);
			sendText.text = "发送邮件";
			sendText.x = 15;
			sendText.y = 30;
			sendText.addEventListener(MouseEvent.MOUSE_OVER, onTextMouseOver);
			sendText.addEventListener(MouseEvent.MOUSE_OUT, onTextMouseOut);
			sendText.addEventListener(MouseEvent.CLICK, sendMail);
			addChild(sendText);
		}
		
		private function onTextMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
		}
		
		private function onTextMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
		}
		
		//显示坦克信息
		private function showTankInfo(event:MouseEvent):void
		{
			var friend:PlayerInfo = PlayerInfoManager.getInstance().players[friendItem.playerId];
			if (friend.id == PlayerInfoManager.getInstance().myUserId)
			{
	        	var popMessage:PopMessage = new PopMessage("不能攻击自己的要塞！", 0xFF0000);
	        	WidgetManager.getInstance().popOut([popMessage], false, true);
			}
			else if (friend.attackedCount >= 3)
			{
	        	popMessage = new PopMessage(friend.nickname + "的要塞今日已经被攻击了3次，放过他吧( ⊙ o ⊙ )", 0xFF0000);
	        	WidgetManager.getInstance().popOut([popMessage], false, true);
			}
			else
			{
				ServerHelper.getInstance().getPlayerInfo(friendItem.playerId);
				ServerHelper.getInstance().addEventListener(ServerEvent.GET_PLAYER_INFO, onShowTankInfoComplete);
			}
		}
		
		private function onShowTankInfoComplete(event:ServerEvent):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.GET_PLAYER_INFO, onShowTankInfoComplete);
			var campIndex:int = 70 * Math.random() + 2;
			var friendDialog:FriendDialog = new FriendDialog(campIndex, friendItem.playerId);
			WidgetManager.getInstance().popOut([friendDialog], true, true);
		}
		
		private function sendMail(event:MouseEvent):void
		{
			if (friendItem.playerId == PlayerInfoManager.getInstance().myUserId)
			{
	        	var popMessage:PopMessage = new PopMessage("不能给自己发邮件！", 0xFF0000);
	        	WidgetManager.getInstance().popOut([popMessage], false, true);
			}
			else
			{
				var sendMailTab:SendMailTab = SendMailTab.getInstance();
				if (sendMailTab.mail == null)
				{
					sendMailTab.mail = new Mail(PlayerInfoManager.getInstance().myUserId.toString(), friendItem.playerId.toString());
				}
				else 
				{
					sendMailTab.mail.setToId(friendItem.playerId.toString());
				}
				sendMailTab.x = 20;
				sendMailTab.y = 100;
				var warehouse:Warehouse = Warehouse.getInstance();
				warehouse.refresh();
				warehouse.x = 366;
				warehouse.y = 100; 
				WidgetManager.getInstance().popOut([warehouse, sendMailTab], true);
			}	
		}
	}
}